import { Component, OnInit } from '@angular/core';
import { ApiService } from 'src/app/core/api';
import { MessageService } from 'src/app/core/service/message.service';
import { FormBuilder, Validators } from '@angular/forms';
import { DatePipe } from '@angular/common';
import { NzModalService, NzMessageService } from 'ng-zorro-antd';
import { I18nService } from 'src/app/core/i18n/i18n.service';

@Component({
    templateUrl: './index.html',
    styles: [
        `
          .tab-head .ant-btn{
            margin-right: 12px;
          }
          .tab-head .ant-input-search-button{
              margin:0;
          }
          
        `
    ],

})
export class UsersComponent implements OnInit {
    page = {
        pageNo: 1,
        pages: 10,
        loginId:"",               // 昵称
        email:"",         		  // 邮箱
        beginTime: '',
        endTime: '',
    };
    dateRange;
    dataList = [];
    isVisible = false;
    isOkLoading = false;
    tableLoading = false;
    modalTitle;
    modal;
    selectedRowData: any = {};
    validateForm;
    constructor(
        private apiService: ApiService,
        private message: MessageService,
        private fb: FormBuilder,
        private date: DatePipe,
        private modalService:NzModalService,
        private nzMessage:NzMessageService,
        private i18n:I18nService
    ) {
        this.i18n.get(['modal','pagetitle']).subscribe(res=>{
            this.modal = res.modal;
            const pagetitle = res.pagetitle;
            this.message.setBreadcrumb({
                breadcrumbs: [{ name: pagetitle.title4}],
            });
        })
    }
    ngOnInit() {
        this.modalTitle = '';
        this.loadList();
    }
    loadList() {
        this.tableLoading = true;
        this.apiService.post('用户列表', { ...this.page }).then((res: any) => {
            this.dataList = res.data.list;
            this.page.pageNo = res.data.pageNo;
            this.page.pages = res.data.pages * 10;
            this.tableLoading = false;
        });
    };
    changeCurrentPage(e) {
        if (e) {
            this.page.pageNo = e;
            this.loadList();
        }
    }
    selRow;
    isDel:boolean;
    handleCancel() {
        this.isVisible = false;
    };
    onChangeStatus(data, s) {
        this.apiService.post('冻结/激活用户', { id: data.id, active: s }).then((res: any) => {
            this.loadList();
        });
    };
    onChangeDate(e) {
        this.page['beginTime'] = e[0] ? this.date.transform(e[0], 'yyyy-MM-dd') : '';
        this.page['endTime'] = e[1] ? this.date.transform(e[1], 'yyyy-MM-dd') : '';
    };
    changeStatus(data,active){
        if(active === 'AUDIT'){
            this.modalService.confirm({
                nzTitle: this.modal.title,
                nzContent: this.modal.userModal1,
                nzOkText: this.modal.userBtn1,
                nzOkType: 'primary',
                nzOnOk: () => {
                    this.apiService.post('冻结/激活用户',{id:data.id,active}).then((res:any)=>{
                        this.nzMessage.success(this.modal.userSuccess1);
                        this.loadList();
                    });
                },
                nzCancelText: this.modal.cancel,
            });
        }else if(active === 'UNAUDIT'){
            this.modalService.confirm({
                nzTitle: this.modal.title,
                nzContent: this.modal.userModal2,
                nzOkText: this.modal.userBtn2,
                nzOkType: 'primary',
                nzOnOk: () => {
                    this.apiService.post('冻结/激活用户',{id:data.id,active}).then((res:any)=>{
                        this.nzMessage.success(this.modal.userSuccess2);
                        this.loadList();
                    });
                },
                nzCancelText: this.modal.cancel,
            });
        }
        
    }
    sherch() {
        this.loadList();
    };
}
